home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_06 / allison / ddir.c < prev    next >
C/C++ Source or Header  |  1994-05-09  |  3KB  |  109 lines

  1. /* ddir.c:  Remove subdirectory tree */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <io.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include <setjmp.h>
  9. #include <dirent.h>
  10. #include <dir.h>
  11.  
  12. /* longjmp return codes */
  13. #define BAD_DIR 1
  14. #define DIR_OPEN_ERR 2
  15. #define FILE_DEL_ERR 3
  16. #define DIR_DEL_ERR 4
  17.  
  18. /* DOS-specific macros - change for other OS */
  19. #define CMD_FORMAT "del *.* <%s > nul"
  20. #define CMD_LEN 17
  21.  
  22. /* Change this to "/" for UNIX */
  23. char Response_file[L_tmpnam+1] = "\\";
  24.  
  25. /* Calling environment buffer */
  26. jmp_buf env;
  27.  
  28.  
  29. main(volatile int argc, char ** volatile argv)
  30. {
  31.     FILE *f;
  32.     volatile char *old_path = getcwd(NULL,64);
  33.     void rd(char *);
  34.  
  35.     /* Create response file for DOS del command */
  36.     tmpnam(Response_file+1);
  37.     if ((f = fopen(Response_file,"w")) == NULL)
  38.         abort();
  39.     fputs("Y\n",f);
  40.     fclose(f);
  41.  
  42.     switch(setjmp(env))
  43.     {
  44.         case BAD_DIR:
  45.             fputs("Invalid directory\n",stderr);
  46.             break;
  47.         case DIR_OPEN_ERR:
  48.             fputs("Error opening directory\n",stderr);
  49.             break;
  50.         case FILE_DEL_ERR:
  51.             fputs("Error deleting file\n",stderr);
  52.             break;
  53.         case DIR_DEL_ERR:
  54.             fputs("Error deleting directory\n",stderr);
  55.             break;
  56.     }
  57.  
  58.     /* Delete the directories */
  59.     while (--argc)
  60.         rd((char *) *++argv);
  61.  
  62.     /* Clean-up */
  63.     remove(Response_file);
  64.     chdir((char *) old_path);
  65.     return 0;
  66. }
  67.  
  68. void rd(char* dir)
  69. {
  70.     char sh_cmd[L_tmpnam+CMD_LEN];
  71.     DIR *dirp;
  72.     struct dirent *entry;
  73.     struct stat finfo;
  74.  
  75.     /* Log onto the directory that is to be deleted */
  76.     if (chdir(dir))
  77.         longjmp(env,BAD_DIR);
  78.     printf("%s:\n",strlwr(dir));
  79.  
  80.     /* Delete all normal files via OS shell */
  81.     sprintf(sh_cmd,CMD_FORMAT,Response_file);
  82.     system(sh_cmd);
  83.  
  84.     /* Delete any remaining directory entries */
  85.     if ((dirp = opendir(".")) == NULL)
  86.         longjmp(env,DIR_OPEN_ERR);
  87.     while ((entry = readdir(dirp)) != NULL)
  88.     {
  89.         if (entry->d_name[0] == '.')
  90.             continue;
  91.         stat(entry->d_name,&finfo);
  92.         if (finfo.st_mode & S_IFDIR)
  93.             rd(entry->d_name);      /* Subdirectory */
  94.         else
  95.         {
  96.             /* Enable delete of file, then do it */
  97.             chmod(entry->d_name,S_IWRITE);
  98.             if (unlink(entry->d_name))
  99.                 longjmp(env,FILE_DEL_ERR);
  100.         }
  101.     }
  102.     closedir(dirp);
  103.  
  104.     /* Remove the directory from its parent */
  105.     chdir("..");
  106.     if (rmdir(dir))
  107.         longjmp(env,DIR_DEL_ERR);
  108. }
  109.